package com.company.jetpack;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.work.Data;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;

import com.company.jetpack.work.ReadWorker;
import com.company.jetpack.work.SecondWorker;
import com.company.jetpack.work.WriteWorker;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    WorkManager workManager;
    Data data;
    OneTimeWorkRequest readSecondWorker;
    OneTimeWorkRequest oneTimeWorkRequest_read;
    OneTimeWorkRequest oneTimeWorkRequest_write;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        workManager = WorkManager.getInstance(this);
        initData();
        addListener();

    }

    private void addListener() {
        findViewById(R.id.btn_read).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                workManager.cancelWorkById(readSecondWorker.getId());
                workManager.cancelWorkById(oneTimeWorkRequest_read.getId());
                oneTimeWorkRequest_read = new OneTimeWorkRequest.Builder(ReadWorker.class).setInputData(data).build();
                readSecondWorker = new OneTimeWorkRequest.Builder(SecondWorker.class).setInputData(data).build();
                workManager.beginWith(oneTimeWorkRequest_read).then(readSecondWorker).enqueue();
                workManagerListener();
            }
        });
        findViewById(R.id.btn_write).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                workManager.cancelWorkById(readSecondWorker.getId());
                workManager.cancelWorkById(oneTimeWorkRequest_write.getId());

                oneTimeWorkRequest_write = new OneTimeWorkRequest.Builder(WriteWorker.class).setInputData(data).build();
                readSecondWorker = new OneTimeWorkRequest.Builder(SecondWorker.class).setInputData(data).build();
                workManager.beginWith(oneTimeWorkRequest_write).then(readSecondWorker).enqueue();
                workManagerListener();

            }
        });
        findViewById(R.id.btn_second).setOnClickListener(new View.OnClickListener() {
            @SuppressLint("RestrictedApi")
            @Override
            public void onClick(View v) {
                readSecondWorker = new OneTimeWorkRequest.Builder(SecondWorker.class).setInputData(data).build();
                workManager.beginWith(readSecondWorker).enqueue();
                workManagerListener();

            }
        });
        findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @SuppressLint("RestrictedApi")
            @Override
            public void onClick(View v) {
                Log.e(TAG, "onClick: cancel");
                workManager.cancelWorkById(readSecondWorker.getId());
            }
        });
    }


    @SuppressLint("RestrictedApi")
    private void initData() {
        int[] a = {1, 2, 3, 4};
        data = new Data.Builder().putIntArray(SecondWorker.KEY, a).build();
        oneTimeWorkRequest_read = new OneTimeWorkRequest.Builder(ReadWorker.class).setInputData(data).build();
        oneTimeWorkRequest_write = new OneTimeWorkRequest.Builder(WriteWorker.class).setInputData(data).build();
        readSecondWorker = new OneTimeWorkRequest.Builder(SecondWorker.class).setInputData(data).build();
    }

    void workManagerListener() {

        workManager.getWorkInfoByIdLiveData(oneTimeWorkRequest_read.getId()).observe(this, new Observer<WorkInfo>() {
            @Override
            public void onChanged(WorkInfo workInfo) {
                // 更新进度处理
                if (workInfo != null && workInfo.getState() == WorkInfo.State.RUNNING) {
                    Data progress = workInfo.getProgress();
                    int value = progress.getInt(ReadWorker.PROGRESS, 0);
                    Log.e(TAG, "onChanged1: read==" + value);
                }
            }
        });
        workManager.getWorkInfoByIdLiveData(oneTimeWorkRequest_write.getId()).observe(this, new Observer<WorkInfo>() {
            @Override
            public void onChanged(WorkInfo workInfo) {

                if (workInfo != null && workInfo.getState() == WorkInfo.State.RUNNING) {
                    // 更新进度处理
                    Data progress = workInfo.getProgress();
                    int value = progress.getInt(ReadWorker.PROGRESS, 0);
                    Log.e(TAG, "onChanged2: write==" + value);
                }
            }
        });

        workManager.getWorkInfoByIdLiveData(readSecondWorker.getId()).observe(this, workInfo -> {
            if (workInfo != null) {

                if (workInfo != null && workInfo.getState() == WorkInfo.State.RUNNING) {
                    // 更新进度处理
                    Data data = workInfo.getProgress();
                    int value = data.getInt(SecondWorker.PROGRESS, 0);
                    Log.e(TAG, "onChanged3: second ==" + value);
                } else if (workInfo != null && workInfo.getState() == WorkInfo.State.SUCCEEDED) {
                    Data outdata = workInfo.getOutputData();
                    int[] out = outdata.getIntArray(SecondWorker.KEY);
                    Log.e(TAG, "onChanged3: SUCCEEDED");
                }
            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "onPause: ");
        workManager.cancelAllWorkByTag("saveRequest");
    }
}